//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "MVTTraining\fn_targetRaiser.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_end","_player_present","_start_building","_end_building","_target","_targets","_front_crossing","_back_crossing","_has_street_targets","_player_in_crossing","_idx"];
_end = false;
waituntil {sleep 5;
	if (GVAR(AUTO_POP_TARGETS) && {GVAR(NEXT_TARGET) < time} && {count GVAR(CROSSINGS_STREET_TARGETS_FRONT) > 0} && {count GVAR(CROSSINGS_STREET_TARGETS_BACK) > 0} && {count GVAR(CROSSINGS_TARGETS) > 0}) then {
		_player_present = false;
		_start_building = -1;
		_end_building = -1;
		for [{_inc = 1},{_inc <= GVAR(BUILDINGS)},{_inc = _inc + 1}] do {
			if (GVAR(BUILDINGS_STATUS) select _inc) then {
				_player_present = true;
				if (_start_building < 0) then { _start_building = _inc };
				_end_building = _inc;
			};
		};
		_end_building = _end_building max _start_building;
		if (_player_present) then {
			TRACE_1("Player Present",ARR_2(_start_building,_end_building));
			if (count GVAR(ACTIVE_TARGETS) > 0) then {
				for [{_inc = 0},{_inc < (count GVAR(ACTIVE_TARGETS))},{_inc = _inc + 1}] do {
					_target = GVAR(ACTIVE_TARGETS) select _inc;
					if (!isNil "_target" && {IS_OBJECT(_target)} && {!isnull _target}) then {
						_is_up = _target getVariable["IsUp",false];
						_up_time = _target getVariable["UpTime",0];
						if (!_is_up || {abs(time - _up_time) > 120}) then {
							TRACE_1("Target Cleanup",ARR_3(_inc,_is_up,_up_time));
							if (abs(time - _up_time) > 120) then {
								GVAR(MISSED_TARGETS) = GVAR(MISSED_TARGETS) + 1;
								_target animate["terc", 1];
								TRACE_1("Target Missed",GVAR(MISSED_TARGETS));
							};
							_target setVariable["IsUp",false];
							_target setVariable["UpTime",0];
							GVAR(ACTIVE_TARGETS) set [_inc, objNull];
						};
					} else {
						GVAR(ACTIVE_TARGETS) set [_inc, objNull];
					};
				};
				GVAR(ACTIVE_TARGETS) = GVAR(ACTIVE_TARGETS) - [objNull];
			};
			TRACE_1("Active Targets",GVAR(ACTIVE_TARGETS));
			if (count GVAR(ACTIVE_TARGETS) < GVAR(MAX_ACTIVE_TARGETS)) then {
				_targets = [];
				
				_player_in_crossing = GVAR(CROSSINGS_STATUS) select _start_building;
				
				_front_crossing = _end_building + 1;
				_back_crossing = _start_building - 2;
				// We check if the players have targets ahead or behind them
				_has_street_targets = (_front_crossing <= GVAR(CROSSINGS)) || (_back_crossing >= 1);
				if (_has_street_targets && {random(10) > 6}) then {
					// Raise a target in front or in the back, depending also on what's available
					if (_front_crossing <= GVAR(CROSSINGS) && {random(10) >= 5}) then {
						if (count (GVAR(CROSSINGS_STREET_TARGETS_FRONT) select _front_crossing) > 0) then {
							TRACE_1("Front Target",_front_crossing);
							_targets = GVAR(CROSSINGS_STREET_TARGETS_FRONT) select _front_crossing;
						};
					} else {
						if (_back_crossing >= 1 && {count (GVAR(CROSSINGS_STREET_TARGETS_BACK) select _back_crossing) > 0}) then {
							TRACE_1("Back Target",_back_crossing);
							_targets = GVAR(CROSSINGS_STREET_TARGETS_BACK) select _back_crossing;
						};
					};
				} else {
					if (_player_in_crossing) then {
						// Raise a target on the side
						TRACE_1("Crossing Target",_start_building);
						_targets = GVAR(CROSSINGS_TARGETS) select _start_building;
					};
				};
				if (IS_ARRAY(_targets) && {count _targets > 0}) then {
					_idx = 0;
					for [{_inc = 0},{_inc < 10},{_inc = _inc + 1}] do {
						_idx = (floor(random(count _targets)));
					};
					TRACE_2("Targets",_idx,_targets);
					_target = _targets select _idx;
					if !(_target getVariable["IsUp",false]) then {
						GVAR(RAISED_TARGETS) = GVAR(RAISED_TARGETS) + 1;
						_target setVariable["IsUp",true];
						_target setVariable["UpTime",time];
						_target animate["terc", 0];
						GVAR(ACTIVE_TARGETS) set [count GVAR(ACTIVE_TARGETS), _target];
						GVAR(NEXT_TARGET) = time + (GVAR(NEXT_TARGET_DELAY) / 2) + (random(GVAR(NEXT_TARGET_DELAY)) / 2);
						TRACE_1("Target Raised",ARR_3(GVAR(RAISED_TARGETS),count GVAR(ACTIVE_TARGETS),GVAR(NEXT_TARGET)));
						#ifdef __FORCE_TRACE__
						hint format["Target Raised %1",count GVAR(ACTIVE_TARGETS)];
						#endif
					};
				};
			};
		};
	};
	_end;
};

TRACE("End");
TMFPROFILERSTOP;
